package homework;

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.*;
import java.util.Iterator;

public class ParseXml {

        private static   File file = new File("F:\\IDEACodeOFJavaEE\\XMLStudy\\src\\homework\\scores.xml");

    public static void reader() {
        //创建SAXReader对象，和xml文件
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(file);
            //获取根元素
            Element rootElement = doc.getRootElement();
            //获取根元素的子元素通过迭代器
            Iterator<Element> eleIt = rootElement.elementIterator();
            while (eleIt.hasNext()) {
                Element element = eleIt.next();
                Attribute atbu = element.attribute("id");
                String strId = atbu.getValue();
                int id = Integer.parseInt(strId);
                Element nameEle = element.element("name");
                String name = nameEle.getText();
                Element courseEle = element.element("course");
                String course = courseEle.getText();
                Element scoreEle = element.element("score");
                String strScore = scoreEle.getText();
                int score = Integer.parseInt(strScore);

                Student student = new Student(id, name, course, score);
                System.out.println(student.toString());
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }

    }
    public static void save(Document document ){
        OutputFormat of = OutputFormat.createPrettyPrint();
        XMLWriter writer = null;
        try {
           writer = new XMLWriter(new FileWriter(file),of);
            writer.write(document);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void delete(){
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(file);
            Element root = doc.getRootElement();
            Iterator<Element> itEle = root.elementIterator();
            while (itEle.hasNext()){
                Element it = itEle.next();
                Attribute attribute = it.attribute("id");
                if(attribute.getValue().equals("1")) {
                    itEle.remove();
                }

            }
            save(doc);
        } catch (DocumentException e) {

        }
    }


    //修改学生信息
    public static void alter(){
    SAXReader reader = new SAXReader();
    Document doc = null;
        try {
            doc = reader.read(file);
            Element root = doc.getRootElement();
            Iterator<Element> rootIt = root.elementIterator();
            while(rootIt.hasNext()){
                Element element = rootIt.next();
                Attribute attribute = element.attribute("id");
                if(attribute.getValue().equals("2")){
                    Iterator<Element> iterator = element.elementIterator();
                    while (iterator.hasNext()){
                       Element element1 = iterator.next();
                       if(element1.getName().equals("score")){
                           element1.setText("60");
                       }
                    }
                }
            }
            save(doc);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

        //添加学生信息
    public static void add(){

        SAXReader reader = new SAXReader();
        Document doc = null;

        try {
            doc = reader.read(file);
            Element rootELe = doc.getRootElement();
            Element stuEle = rootELe.addElement("student");
            stuEle.addAttribute("id","5");
            Element nameEle = stuEle.addElement("name");
            nameEle.setText("狗涛");

            Element courseEle = stuEle.addElement("course");
            courseEle.setText("高数");

            Element scoreEle = stuEle.addElement("score");
            scoreEle.setText("78");
            save(doc);
        } catch (DocumentException e) {
            e.printStackTrace();
        }


    }













    public static void main(String[] args){
        //alter();
        //delete();
        add();
        }
}
